package tt.dz.biz.service.thirdaccount;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;
import tt.dz.biz.dao.thirdaccount.ThirdAccountDaoImpl;
import tt.dz.common.BusinessException;
import tt.dz.common.DzErrorCode;
import tt.dz.common.DzErrorCode.ThirdAccountErrorCode;
import tt.dz.common.LogUtil;
import tt.dz.vo.ThirdAccountVo;

@Service("thirdAccountService")
public class ThirdAccountService
{
  @Autowired
  @Qualifier("thirdAccountDao")
  private ThirdAccountDaoImpl thirdAccountDao;
  
  public boolean deleteThirdAccount(String userId, String bindType, String thirdUuuid)
  {
    ThirdAccountVo vo = new ThirdAccountVo();
    
    vo.setUserId(userId);
    vo.setBindType(bindType);
    vo.setThirdUuuid(thirdUuuid);
    
    return this.thirdAccountDao.deleteThirdAccountVo(vo) > 1;
  }
  
  public ThirdAccountVo queryThirdAccountByUserIdAndBindTypeAndThirdUUid(String userId, String bindType, String thirdUuuid)
  {
    ThirdAccountVo vo = null;
    try
    {
      vo = this.thirdAccountDao.queryByUserIdAndBindTypeAndThirdUuuid(userId, bindType, thirdUuuid);
    }
    catch (EmptyResultDataAccessException e)
    {
      return null;
    }
    return vo;
  }
  
  public List<ThirdAccountVo> queryThirdAccoutByUserIdAndBindType(String userId, String bindType)
  {
    List<ThirdAccountVo> list = null;
    try
    {
      list = this.thirdAccountDao.queryThirdAccoutByUserIdAndBindType(userId, bindType);
    }
    catch (Exception e)
    {
      list = new ArrayList(0);
    }
    return list;
  }
  
  public boolean save(ThirdAccountVo vo)
  {
    boolean isSuccess = false;
    BusinessException businessException = null;
    try
    {
      int i = this.thirdAccountDao.saveThirdAccountVo(vo);
      
      isSuccess = i > 0;
    }
    catch (DuplicateKeyException e)
    {
      businessException = new BusinessException(DzErrorCode.ThirdAccountErrorCode.REPEAT_BIND.value(), DzErrorCode.ThirdAccountErrorCode.REPEAT_BIND.getDesc(), e.getLocalizedMessage());
    }
    catch (DataIntegrityViolationException e)
    {
      businessException = new BusinessException(DzErrorCode.ThirdAccountErrorCode.BIND_PARAM_MISS.value(), DzErrorCode.ThirdAccountErrorCode.BIND_PARAM_MISS.getDesc(), e.getLocalizedMessage());
    }
    catch (Exception e)
    {
      businessException = new BusinessException("保存第三方账户异常" + e.getLocalizedMessage());
    }
    if (businessException != null)
    {
      LogUtil.error("保存第三方账户异常--入参userId=" + vo.getUserId() + "; thirdUuuid=" + vo.getThirdUuuid() + " ", businessException);
      

      throw businessException;
    }
    return isSuccess;
  }
  
  public boolean updateThirdAccounnt(String userId, String bindType, String oldThirdUuuid, String newThirdUuuid)
  {
    BusinessException businessException = null;
    int i = this.thirdAccountDao.updateBindThirdAccountVo(userId, bindType, oldThirdUuuid, newThirdUuuid);
    



    return i > 0;
  }
  
  public void deteleAccounts(ThirdAccountVo... thirdAccountVos)
    throws BusinessException
  {
    try
    {
      this.thirdAccountDao.bathDeteleAccounts(thirdAccountVos);
    }
    catch (DataAccessException e)
    {
      throw new BusinessException(DzErrorCode.ThirdAccountErrorCode.DELETE_ACCOUNT_FAILTURE.value(), DzErrorCode.ThirdAccountErrorCode.DELETE_ACCOUNT_FAILTURE.getDesc(), e.getLocalizedMessage());
    }
  }
  
  public List<ThirdAccountVo> queryThirdAccoutByUserId(String userId)
  {
    return this.thirdAccountDao.queryByUserId(userId);
  }
  
  public List<ThirdAccountVo> queryThirdAccuntByBindTypeAndThirdUuuidAndMerchantCode(String bindType, String thirdUuuid, String merchantCode)
  {
    return this.thirdAccountDao.queryThirdAccuntByBindTypeAndThirdUuuidAndMerchantCode(bindType, thirdUuuid, merchantCode);
  }
  
  public boolean isExistUUUIDUserIdRepeat(String userId, String thirdUuuid)
  {
    return this.thirdAccountDao.isExistUUUIDUserIdRepeat(userId, thirdUuuid);
  }
  
  public int queryThirdAccuntByBindTypeAndThirdUuuidAndMerchantCodeCount(String bindType, String thirdUuuid, String merchantCode)
  {
    return this.thirdAccountDao.queryThirdAccuntByBindTypeAndThirdUuuidAndMerchantCodeCount(bindType, thirdUuuid, merchantCode);
  }
}
